package me.midday.java.base.knowledge.mutlthread;

class A{
    public synchronized void foo(B b){
        System.out.println("当前线程：" + Thread.currentThread().getName() + "进入实例A的foo方法");
        try{
            Thread.sleep(200);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        System.out.println("当前 线程:" + Thread.currentThread().getName() + "企图调用B的last 方法" );
        b.last();
    }
    public synchronized void last(){
        System.out.println("进入A类的last()方法");
    }
}
class B{
    public synchronized void bar(A a){
        System.out.println("当前线程名："+ Thread.currentThread().getName() + "进入了B实例的bar方法");
        try{
            Thread.sleep(200);
        }catch (InterruptedException ex){
            ex.printStackTrace();
        }
        System.out.println("当前线程" + Thread.currentThread().getName()+ "企图调用A的last 方法");
        a.last();
    }
    public synchronized void last(){
        System.out.println("进入B类的last()方法");
    }

}
public class DeadLock implements Runnable{
    A a = new A();
    B b = new B();
    public void init(){
        Thread.currentThread().setName("主线程");
        a.foo(b);
        System.out.println("进入主线程之后");
    }


    @Override
    public void run() {
        Thread.currentThread().setName("副线程");
        b.bar(a);
        System.out.println("进入副线程之后");
    }

    public static void main(String[] args){
        DeadLock d1 = new DeadLock();
        new Thread(d1).start();
        d1.init();
    }
}
